﻿using System.ComponentModel.Composition;
using System.Threading;
using Cubicle.Core.Interfaces;

namespace Cubicle.Core.Processors.Invoke
{
    [Export("Cubicle.Processors.Invoke.InstructionMapProcessor", typeof(IProcessor<Instruction>))]
    [ExportMetadata("Name", "InstructionMapProcessor")]
    [ExportMetadata("Description", "Maps Instructions -> InstructionRequests")]
    public class InstructionMapProcessor : ProcessorQueue<Instruction, InstructionRequest>
    {

        protected override InstructionRequest ConvertTo(Instruction request)
        {
            return new InstructionRequest(request) { ExecutingThread = Thread.CurrentThread };
        }

        protected override Instruction ConvertFrom(InstructionRequest input)
        {
            input.Inst.Result = input.Result();
            return input.Inst;
        }
    }

    [Export("Cubicle.Processors.Invoke.InstructionCastProcessor", typeof(IProcessor<Instruction>))]
    [ExportMetadata("Name", "InstructionMapProcessor")]
    [ExportMetadata("Description", "Maps Instructions -> InstructionRequests")]
    public class InstructionCastProcessor : ProcessorQueue<object, Instruction>
    {

        protected override Instruction ConvertTo(object request)
        {
            return (Instruction)request;
        }

        protected override object ConvertFrom(Instruction input)
        {
            return input.Result;
        }
    }
}